/**
 * 
 */
package com.sc.softmgr.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.acc.softmgr.model.SysUser;

/**
 * 
 * @content Session拦截器
 * 
 * @date 2017年5月16日
 * 
 * @author OprCalf
 * 
 */
public class SessionInterceptor extends HandlerInterceptorAdapter {

	private static final Logger log = LoggerFactory.getLogger(SessionInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		log.info("开始设置页面编码");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		log.info("开始拦截：" + request.getRequestURI());
		Object obj = request.getSession().getAttribute("currentUser");
		if (obj == null || !(obj instanceof SysUser)) {
			response.sendRedirect("/");
			return false;
		}

		return true;
	}

	@Override
	public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
			ModelAndView modelAndView) throws Exception {

	}

	@Override
	public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			Object o, Exception e) throws Exception {

	}
}
